/**
 * Created by dinghh on 2018-7-11.
 */
angular.module('MetronicApp').controller('ModelManagementListController',
    ['$rootScope', '$scope', '$state', '$uibModal', 'toastr','$location', 'FileStorageService', 'ModelManagementService',
        function ($rootScope, $scope, $state, $uibModal, toastr,$location, FileStorageService, ModelManagementService) {
            $scope.$on('$viewContentLoaded', function () {
                App.initAjax();
                $rootScope.settings.layout.pageBodySolid = true;
                $rootScope.settings.layout.pageSidebarClosed = false;
            });

            $scope.columns = ModelManagementService.getSchema();
            $scope.pageable = ModelManagementService.getPageable();

            $scope.modelList = [];

            //tab页切换
            $scope.tabItemList = [
                {
                    "name": "模型管理",
                    "router": "modelManagementList",
                    "selected": true
                },
                {
                    "name": "污染源数据造假识别",
                    "router": "recognitionDetail",
                    "selected": false
                }
            ];

            $scope.$watch('pageable.size', function (newVal, oldVal) {
                if (newVal === oldVal) return;
                ModelManagementService.setSize(newVal);
                $scope.list();
            });
            $scope.$watch('pageable.number', function (newVal, oldVal) {
                if (newVal === oldVal) return;
                ModelManagementService.setStoredPage(newVal);
                $scope.list();
            });

            $scope.condition = {};

            $scope.list = function () {
                ModelManagementService.putSearchParams({
                    modelName: $scope.condition.modelName,
                    search: $scope.condition.search
                });
                ModelManagementService.list(function (res) {
                    if ('success' === res.status) {
                        $scope.rows = res.data;
                        $scope.pageable = res.pageable;
                    } else {
                        for (var i = 0; i < res.errors.length; i ++) {
                            toastr.error(res.errors[i].errmsg, "数据获取失败");
                        }
                    }
                });
            };

            var gotoFirstPage = function () {
                ModelManagementService.setStoredPage(0);
                $scope.list();
                ModelManagementService.clearSearchParams();
            };
            gotoFirstPage();

            //查询
            $scope.search = function () {
                gotoFirstPage();
            };

            //编辑
            // $scope.edit = function (row) {
            //     $state.go('modelManagementEdit', {id: row.id});
            // };

            //配置
            // $scope.deploy = function (row) {
            //     $state.go('modelManagementDeploy', {id: row.id});
            // };

        }
    ]
);